package com.zhz.lib.cms.manage.feedback;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zhz.lib.cms.domain.feedback.Feedbacklib;
import com.zhz.lib.cms.manage.BaseActionWithApprove;
import com.zhz.lib.cms.utils.zhz.ZhzSecurityUtil;
import com.zhz.lib.cms.utils.zhz.ZhzSortMapUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

public class feedbackAction extends BaseActionWithApprove {
    private static String TABLE      = "Feedbacklib";
    private static String KEY        = "feedbackID";
    private static String LIST       = "feebacklist";
    private static String ObjectName = "feedbacklib";

    private Integer       newsfromfatherid;

    /**
     * 不给参数，默认执行此处
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {

        return feedbacklist(mapping, form, request, response);

    }

    /**
     * 显示留言列表
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward feedbacklist(ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request, HttpServletResponse response)
                                                                                               throws Exception {
        StringBuffer fanyeHref = new StringBuffer();
        fanyeHref.append("feedbackManage.do?method=feedbacklist");

        getListWithFanyeHref(request, TABLE, BaseDAO.WHERE + BaseDAO.WHERE_DELETE, " order by "
                                                                                   + KEY + " DESC",
            LIST, fanyeHref);

        return mapping.findForward("Feedback-list");
    }

    /**
     * 回复留言
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward reply(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                               HttpServletResponse response) throws Exception {

        Feedbacklib feedbacklib = (Feedbacklib) getRecord(request, TABLE, KEY, ObjectName);

        if (feedbacklib.getAdminfrom() != null) {
            request.setAttribute("department", ZhzSortMapUtil.getSortmap(
                feedbacklib.getAdminfrom(), baseDao));
            request.setAttribute("reply", "reply");
        }

        request.setAttribute("newsfrominfolist", ZhzSortMapUtil.getChildSortMapList(
            newsfromfatherid, baseDao));

        return mapping.findForward("Feedback-reply");

    }

    /**
     * 保存回复
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward replys(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String feedbackID = ZhzHttpRequestUtil.getParameters(request, "feedbackID");
        String reply = ZhzHttpRequestUtil.getParameters(request, "reply");
        String adminfrom = ZhzHttpRequestUtil.getParameters(request, "adminfrom");

        Feedbacklib feedbacklib = (Feedbacklib) baseDao.findOneObject(TABLE, KEY, feedbackID);

        feedbacklib.setReply(reply);
        feedbacklib.setReplydate(new Date());
        feedbacklib.setAdmin(ZhzSecurityUtil.getOperatorName());
        feedbacklib.setAdminfrom(adminfrom);
        feedbacklib.setEditor(ZhzSecurityUtil.getOperatorName());
        feedbacklib.setEditorTime(new Date());

        boolean update = baseDao.update(feedbacklib);

        if (update) {
            request.setAttribute("editresult", "editresult");
            request.setAttribute("edit", "edit");
        }

        return mapping.findForward("Feedback-result");
    }

    /**
     * 删除留言
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {

        delete(request, TABLE, KEY);
        return mapping.findForward("Feedback-result");
    }

    /**
     * 批量删除
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward deleteAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {

        deleteAll(request, TABLE, KEY);
        return mapping.findForward("Feedback-result");
    }
    
    /**
     * 对选择的留言进行审核
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward feedbackApprove(ActionMapping mapping, ActionForm form,
                                         HttpServletRequest request, HttpServletResponse response)
                                                                                                  throws Exception {

        //TODO 未回复的不应该审核通过
        approveRecord(request, TABLE, KEY);
        return mapping.findForward("Feedback-result");
    }

    public void setNewsfromfatherid(Integer newsfromfatherid) {
        this.newsfromfatherid = newsfromfatherid;
    }

}